package ssi.staaar;

import ssi.staar.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;

public class AddMessageActivity extends Activity {
	private LocationManager locManager;
	private LocationListener locListener;
	private String receiver;
	private AlertDialog.Builder builder;
	private double currentLat;
	private double currentLong;
	private double currentAlt;
	
	public void cancelFields(View view) {
		EditText subText = (EditText)findViewById(R.id.addMessageSubjectEdit);
		subText.setText("");
		EditText text = (EditText)findViewById(R.id.addMessageTextEdit);
		text.setText("");
	}

	private void init() {
		Intent myIntent = this.getIntent();
		String rec = (String) myIntent.getStringExtra("Receiver");
		if (rec == null)
			receiver = "";
		else {
			receiver = rec;
			CheckBox check = ((CheckBox)findViewById(R.id.addMessageCryptCheck));
			check.setEnabled(false);
		}
	}

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.addmessage);
		builder = new AlertDialog.Builder(this);
		builder.setTitle(R.string.app_name)
			.setMessage(R.string.subject_exceed)
			.setNeutralButton("OK", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {
					finish();
				}
	    });
		locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		locListener = new LocationListener() {
			@Override
			public void onStatusChanged(String provider, int status, Bundle extras) {				
			}
			
			@Override
			public void onProviderEnabled(String provider) {				
			}
			
			@Override
			public void onProviderDisabled(String provider) {
			}
			
			@Override
			public void onLocationChanged(Location location) {
				updateCoordinates(location);
			}
		};
		init();
		startLocationUpdates();
	}
	
	public void onPause() {
		super.onPause();
		Intent myIntent = this.getIntent();
		myIntent.removeExtra("Receiver");
		stopLocationUpdates();
	}
	
	public void onResume() {
		super.onResume();
		init();
		restartLocationUpdates();
	}

	public void onStop() {
		super.onStop();
		stopLocationUpdates();
		Intent myIntent = this.getIntent();
		myIntent.removeExtra("Receiver");
	}
	
	private void restartLocationUpdates() {
		Log.d("[ADDMESSAGE]", "Restarting location updates");
		if (locManager != null && locListener != null) {
			locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locListener);
			locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);
		}
	}

	public void sendMessage(View view) {
		EditText subText = (EditText)findViewById(R.id.addMessageSubjectEdit);
		EditText text = (EditText)findViewById(R.id.addMessageTextEdit);
		CheckBox check = ((CheckBox)findViewById(R.id.addMessageCryptCheck));
		boolean finish = true;
		boolean crypt = check.isEnabled() && check.isChecked();
		if (receiver == null) { 
			receiver = "";
			crypt = false;
		}
		if (subText.getText().toString().length() < 30)
			Staaar.stControl.sendMessage(subText.getText().toString(), text.getText().toString(), 
				currentLat, currentLong, currentAlt, receiver, crypt);
		else {
			finish = false;
			builder.create().show();
		}
		if (finish) { 
			locManager.removeUpdates(locListener);
			this.finish();
		}
	}

	private void startLocationUpdates() {
		Log.d("[ADDMESSAGE]", "Starting location updates");
		locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locListener);
		locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);
	}

	private void stopLocationUpdates() {
		Log.d("[ADDMESSAGE]", "Stopping location updates");
		locManager.removeUpdates(locListener);
	}
	
	protected void updateCoordinates(Location location) {
		currentLat = location.getLatitude();
		currentLong = location.getLongitude();
		currentAlt = location.getAltitude();
	}
}
